package com.yaorange.web;


import com.github.pagehelper.PageInfo;
import com.yaorange.entity.Module;
import com.yaorange.service.ModuleService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@CrossOrigin
@RequestMapping("/system/module")
public class ModuleController {

    @Resource
    private ModuleService moduleService;



    //包含模糊查询的分页
    @GetMapping({"/page/{pageNum}/{pageSize}/{conditionModuleName}","/page/{pageNum}/{pageSize}"})
    @ResponseBody//将后台数据转成json给前端用
    public PageInfo<Module> getPages(@PathVariable Integer pageNum, @PathVariable Integer pageSize,
                                   @PathVariable(required = false) String conditionModuleName){
        PageInfo<Module> deptPageInfo=moduleService.getPageByCondition(pageNum,pageSize,conditionModuleName);
        return deptPageInfo;
    }



    //删
    @DeleteMapping("/deleteModule/{moduleId}")
    @ResponseBody
    public HashMap<String,Object> removeModule(@PathVariable Integer moduleId){
        List<Module> moduleList = moduleService.getModule(moduleId);
        Module module = moduleList.get(0);
        HashMap<String, Object> hashMap = new HashMap<>();
        if(module==null){
            hashMap.put("msg","操作的部门不存在，请刷新页面");
            return hashMap;
        }
        module.setModuleState((byte) 1);
        module.setUpdateTime(new Date());
        int rows=moduleService.updateModule(module);
        if (rows > 0){
            hashMap.put("status",20000);
            hashMap.put("msg","删除模块成功");
        }else {
            hashMap.put("status",30000);
            hashMap.put("msg","删除模块失败");
        }
        return hashMap;
    }




    //改
    //先查询module是否存在
    @GetMapping("/selectModule/{moduleId}")
    @ResponseBody
    public Module getModule(@PathVariable Integer moduleId){
        Module module = moduleService.getModule(moduleId).get(0);
        return module;
    }

    //查询父模块以回显
    @GetMapping("/getParentList")
    public List<Module> getModule(){
        List<Module> parentList = moduleService.getParentList();
        return parentList;
    }

    //再进行修改操作
    @PutMapping("/updateModule")
    @ResponseBody
    public HashMap<String,Object> updateModule(@RequestBody Module module){
        int i = moduleService.updateModule(module);
        HashMap<String, Object> hashMap = new HashMap<>();
        if (i>0){
            hashMap.put("status",20000);
            hashMap.put("msg","修改模块成功");
        }else {
            hashMap.put("status",30000);
            hashMap.put("msg","修改模块失败");
        }
        return hashMap;
    }

}
